﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using ConsoleApplication1;
using ClassLibrary1.entity;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            foreach(var i in Data.types)
            combobox_entity.Items.Add(i.Name);
            combobox_entity.Items.Add("all");

        }

        void Indicator(Thread t)
        {
            progressbar.Value = 0;
            while (CProgram.indic == 0)
            {
                progressbar.Value++;
                Thread.Sleep(800);
            }
            progressbar.Maximum = CProgram.indic + progressbar.Value;
            for (int i = 0; i < CProgram.indic; i++)
            {
                progressbar.Value++;
                Thread.Sleep(400);
            }
            CProgram.indic = 0;
        }

        private void b_add_Click(object sender, EventArgs e)
        {
            new Form2(this).Show();
        }

        private void b_remove_Click(object sender, EventArgs e)
        {
            if (lbox_list.SelectedItem != null)
            {
                string s = lbox_list.SelectedItem.ToString();
                string[] ss = s.Split(' ');
                try
                {
                    CProgram.RunCommand(new string[] { "delete", ss[0], ss[1].Substring(3) });
                }
                catch (Exception) { }
                
                cbox_entity_SelectedIndexChanged(null, EventArgs.Empty);
            }
        }

        private void b_update_Click(object sender, EventArgs e)
        {
            if (lbox_list.SelectedItem != null)
            {
                string s = lbox_list.SelectedItem.ToString();
                string[] ss = s.Split(' ');
                try
                {
                    Type t = CProgram.types[ss[0].ToLower()];
                    int.Parse(ss[1].Substring(3));
                    new Form3(this,t, ss[1].Substring(3)).Show();
                }
                catch (Exception) { }
            }
        }

        private void b_read_Click(object sender, EventArgs e)
        {
            /*Thread t = new Thread(CProgram.LoadXml);
            t.Start();
            Indicator(t);
            */
            CProgram.LoadXml();
            combobox_entity.SelectedItem = "all";

        }

        private void b_write_Click(object sender, EventArgs e)
        {
            /*Thread t = new Thread(CProgram.WriteEntity);
            t.Start();

            Indicator(t);*/
            CProgram.WriteXml();
        }

        void lboxUpdate(List<BaseEntity> list)
        {
            lbox_list.Items.Clear();
            foreach (BaseEntity i in list)
            {
                lbox_list.Items.Add(i.Show());
            }
        }

        public void cbox_entity_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (combobox_entity.Items[combobox_entity.SelectedIndex].ToString() == "all")
            {
                lboxUpdate(Data.entity);
                return;
            }
            try
            {
                var l = Data.entity.FindAll(item => item.GetType() == CProgram.types[combobox_entity.Items[combobox_entity.SelectedIndex].ToString().ToLower()]);
                if (l != null) lboxUpdate(l);
            }
            catch (Exception) { }
        }
    }
}